package cn.com.freelibra.forum.interceptor;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.com.freelibra.forum.po.Keywords;
import cn.com.freelibra.forum.service.KeywordsService;
import cn.com.freelibra.forum.web.method.NewsMessage;
import cn.com.freelibra.forum.web.view.NewsMessageObject;

public class AllInterceptor implements HandlerInterceptor{
	
	@Resource
	private KeywordsService keywordsService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		List<NewsMessageObject> newsMessageList = (List<NewsMessageObject>)request.getSession().getAttribute("newsMessageList");
		List<Keywords> keywordsList = (List<Keywords>)request.getSession().getAttribute("Search_KeywordsList");
		//获取新闻资讯
		if(newsMessageList == null || newsMessageList.size()==0){
			newsMessageList=NewsMessage.getNewMessages();
			request.getSession().setAttribute("newsMessageList", newsMessageList);
		}
		//获取热门搜索
		if(keywordsList==null||keywordsList.size()==0){
			request.getSession().setAttribute("Search_KeywordsList", keywordsService.selectAll());
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

}
